<?php

namespace App\Http\Controllers\Mobile\Company\Auth;
use App\Facades\Style;
use App\Models\Company\CompanyLoginRecord;
use App\Models\Company\CompanyUser;
use App\Http\Requests\Company\Login\AjaxCompanyLoginRequest;
use App\Http\Controllers\Controller;
use App\Models\Company\CompanyVisitRecord;
use App\Models\Personal\PersonalLoginRecord;
use Illuminate\Http\Request;
use Auth,Site;
use Illuminate\Support\Facades\Session;
define('__QQAPITYPE__','person');
require_once('../app/Vendor/QqConnect/API/qqConnectAPI.php');
class QQLoginController extends Controller{

    /**
     * QQ绑定现有企业账号
     * @param AjaxLoginRequest $request
     */
    public function qqBinDing(Request $request){
        $userImg = session('userImg');
        $usname = session('usname');
        Session::forget('usname');
        if(!empty($request->openid)){
            $openid = $request->openid;
            empty($request->unionid) ? $unionid = '' : $unionid = $request->unionid;
            $formUrl = url("mobile/company/stroyqqbinding");
            $category = 'qq';
            return Style::view("mobile.company.auth.binDing",compact('userImg','usname','openid','unionid','formUrl','category'));
        }else{
            return redirect('mobile/company/qqlogin');
        }
    }

    /**
     * 保存企业QQ绑定
     * @param AjaxCompanyLoginRequest $request
     * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function stroyQQBinDing(AjaxCompanyLoginRequest $request){
        $request->only('username','password');
        $request->flash();
        if(Auth::guard('company')->attempt(['username' => $request->username, 'password' => $request->password])){
            $user = Auth::guard('company')->user();
            if(!Session::has('openId')){
                return redirect('mobile/company/qqlogin');
            }
            if(CompanyUser::where('uid',$user->uid)->update(['qqopenid'=>session('openId')])){
                Session::forget('openId');
                Session::forget('userImg');
                //登录记录表
                $this->loginRecord($user);
                return redirect('mobile/company/welcome');
            }else{
                return back()->withErrors(['handleErr'=>'绑定失败，请稍候再试！']);
            }
        }
    }
    /**
     * 登录记录信息
     * @param $userInfo
     */
    public function loginRecord($userInfo){
        //登录记录表
        $record = array();
        $record['uid'] = $userInfo->uid;
        $record['company_login_ip'] = getIp();
        $record['company_login_time'] = date('Y-m-d H:i:s',time());
        $record['login_type'] = 4;
        $record['type'] = 2;
        //登录记录表
        CompanyLoginRecord::create($record);
        //更新用户表
        $currtime = date('Y-m-d H:i:s',time());
        $fifedate = date('Y-m-d H:i:s',strtotime($userInfo->surpris_visit_time . "+15 day"));
        if($fifedate < $currtime ){
            $userInfo->surpris_visit_time = date('Y-m-d H:i:s',time());
            //15天内插入突访记录表
            CompanyVisitRecord::create($record);
        }
        $userInfo->last_login_time = date('Y-m-d H:i:s',time());
        $userInfo->last_login_ip = getIp();
        $userInfo->save();
    }
}